package ZhongXinFenPai;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.grey_zoo.lottery.R;
import com.grey_zoo.lottery.global.GlobalValue;

/**
 * Created by Administrator on 2015/9/24 0024.
 */

/**
 * 管理Activity之间通信，和打开等事�?
 */
public class ActivityEventManager implements TObserver {

	private JSONObject jsonObject;
	/**
	 * nickname：Activity�?
	 */
	private Map<String, String> map = new HashMap<String, String>();
	/**
	 * Activity类：加载的类
	 */
	private Map<String, Class> classMap = new HashMap<String, Class>();

	private static ActivityEventManager instance = new ActivityEventManager();

	public static ActivityEventManager getInstance() {
		return instance;
	}

	private ActivityEventManager() {
		System.out.println("3333");
		String jsonStr = readFile();
		System.out.println("4444");
		jsonObject = JSON.parseObject(jsonStr);
		System.out.println("555555");
		parseJson();
		System.out.println("666666");
		registerEvent();
	}

	/**
	 * 从JSON文件中拿到配置项
	 */
	private void parseJson() {
		JSONArray jsonArray = jsonObject.getJSONArray("Activitys");
		for (int i = 0; i < jsonArray.size(); ++i) {
			JSONObject object = (JSONObject) jsonArray.get(i);
			for (String key : object.keySet()) {
				map.put(key, object.getString(key));
			}
		}
	}

	@Override
	public void onEvent(final TEvent event, final TCallback callback)
			throws NoSuchMethodException, Exception, Exception {
		if (event == null) {
			return;
		}

		/*// �?启Activity事件
		if (event instanceof StartActivityEvent) {
			StartActivityEvent startActivityEvent = (StartActivityEvent) event;
			String clazz = map.get(startActivityEvent.model);
			if (null != clazz) {
				try {
					Class c;
					if (null != classMap.get(clazz)) {
						c = classMap.get(clazz);
					} else {
						c = Class.forName(clazz);
						classMap.put(clazz, c);

					}
					Looper.prepare();
					Constructor constructor = c.getConstructor(Context.class);

					FragmentActivity ac = GlobalValue.activity;
					final TObserver observer = (TObserver) constructor
							.newInstance(ac);

					try {

						changeActivity((HomeUI) observer, new TCallback() {
							@Override
							public void onSuccess(Object result)
									throws Exception {
								// 成功了，分派事件
								try {

									observer.onEvent(event, callback);

								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}

							@Override
							public void onError(String srrMsg) {
								// 失败�?
							}
						});
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}

		Looper.loop();

		Looper.myLooper().quit();*/
	}

	/**
	 * 注册事件
	 */
	public void registerEvent() {
		// 注册�?始Activity事件
		TIntent.getInstance().registerEvent(StartActivityEvent.class, this);
		//TIntent.getInstance().registerEvent("StartActivityEvent.class, this);
	}

	/**
	 * 读取配置文件
	 *
	 * @return
	 */
	public String readFile() {
		StringBuffer buffer = new StringBuffer();
		BufferedReader reader = null;
		try {
			// reader = new BufferedReader(new
			// FileReader("raw/activitylist.json"));
			InputStream inputStream = GlobalValue.application.getResources()
					.openRawResource(R.raw.activitylist);
			reader = new BufferedReader(new InputStreamReader(inputStream));
			String str;
			while (null != (str = reader.readLine())) {
				buffer.append(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return buffer.toString();
	}

	/*
	 * 派发切换Activity事件
	 * 
	 * @param to
	 */
	public <T> void changeActivity(String to, TCallback callback) {
		ChangeActivityEvent event = new ChangeActivityEvent();
		event.to = to;
		TIntent.getInstance().dispatch(event, callback);
	}

}
